//  
//  BGOEvent.m
//  Bango iOS API
//
//  Created by William Shakour on September 8, 2011.
//  Copyrights © 2011 Bango - http://www.bango.net/ - All rights reserved.
//

#import "BGOEvent.h"
#import "BGOEventParameter.h"
#import "BGOProfile.h"
#import "BGOHelperUtils.h"
#import "BGOFieldNameConstants.h"

@implementation BGOEvent
+ (BGOEvent *)event {
  return [[[BGOEvent alloc] init] autorelease];
}
@dynamic eventParameters;
- (void)addToEventParameters:(BGOEventParameter *)eventParameter {
  if (_eventParameters == nil) {
    _eventParameters = [[NSMutableArray array] retain];
  }
  
  [_eventParameters addObject:[eventParameter toJson]];
}
- (NSArray *)eventParameters {
  NSMutableArray * eventParameters = nil;
  
  if (_eventParameters != nil) {
    eventParameters = [NSMutableArray array];
    
    for (NSDictionary *objectEventParameter in _eventParameters) {
      BGOEventParameter *eventParameter = [BGOEventParameter eventParameter];
      [eventParameter fromJson:objectEventParameter];
      [eventParameters addObject:eventParameter];
    }
  }
  
  return eventParameters;
}
@synthesize profile=_profile;
@synthesize eventName=_eventName;
@synthesize eventDetail=_eventDetail;
@dynamic eventTime;
- (NSDate *) eventTime {
  return [NSDate dateWithTimeIntervalSince1970:[_eventTime doubleValue] / 1000.0];
}
- (void) setEventTime:(NSDate *)val_ue {
  if (_eventTime != nil) {
    [_eventTime release];
  }
  _eventTime = [[NSNumber numberWithDouble:[val_ue timeIntervalSince1970] * 1000.0] retain];
}
@synthesize connectionType=_connectionType;
@synthesize operatorConnectionType=_operatorConnectionType;
@synthesize errorId=_errorId;
@dynamic errorLevel;
- (int) errorLevel {
  return [_errorLevel intValue];
}
- (void) setErrorLevel:(int)val_ue {
  if (_errorLevel != nil) {
    [_errorLevel release];
  }
  _errorLevel = [[NSNumber numberWithInt:val_ue] retain];
}
@synthesize eventValue=_eventValue;
@dynamic eventPosted;
- (BOOL) isEventPosted {
  return [_eventPosted boolValue];
}
- (void) setEventPosted:(BOOL)val_ue {
  if (_eventPosted != nil) {
    [_eventPosted release];
  }
  _eventPosted = [[NSNumber numberWithBool:val_ue] retain];
}
- (NSMutableDictionary *) toJson {
  NSMutableDictionary * jsonObject = [super toJson];
  if (_eventParameters != nil) {
    [jsonObject setObject:_eventParameters forKey:BGO_POST_EVENT_PARAMETERS];
  }
//  if (_profile != nil) {
//    [jsonObject setObject:_profile forKey:@"Profile"];
//  }
  if (_eventName != nil) {
    [jsonObject setObject:_eventName forKey:BGO_POST_EVENTNAME];
  }
  if (_eventDetail != nil) {
    [jsonObject setObject:_eventDetail forKey:BGO_POST_EVENTDETAIL];
  }
  if (_eventTime != nil) {
    [jsonObject setObject:[BGOHelperUtils getUTCDate:self.eventTime]
                         forKey:BGO_POST_EVENTTIME];	
    [jsonObject setObject:[BGOHelperUtils getLocalDate:self.eventTime]
                         forKey:BGO_POST_EVENTLOCALTIME];
  }
  if (_connectionType != nil) {
    [jsonObject setObject:_connectionType forKey:BGO_POST_CONNECTIONTYPE];
  }
  if (_operatorConnectionType != nil) {
    [jsonObject setObject:_operatorConnectionType 
                   forKey:BGO_POST_OPERATORCONNECTIONTYPE];
  }
  if (_errorId != nil) {
    [jsonObject setObject:_errorId forKey:BGO_POST_ERRORID];
  }
  if (_errorLevel != nil) {
    [jsonObject setObject:_errorLevel forKey:BGO_POST_ERRORLEVEL];
  }
  if (_eventValue != nil) {
    [jsonObject setObject:_eventValue forKey:BGO_POST_EVENTVALUE];
  }
//  if (_eventPosted != nil) {
//    [jsonObject setObject:_eventPosted forKey:BGO_POST_even];
//  }
  return jsonObject;
}

- (void)fromJson:(NSDictionary *)jsonObject {
  [super fromJson:jsonObject];
  if ([jsonObject objectForKey:BGO_POST_EVENT_PARAMETERS] != nil) {
    _eventParameters = [[NSMutableArray arrayWithArray:[jsonObject objectForKey:BGO_POST_EVENT_PARAMETERS]] retain];
  }
  if ([jsonObject objectForKey:@"profile"] != nil) {
    _profile = [[jsonObject objectForKey:@"profile"] retain];
  }
  if ([jsonObject objectForKey:BGO_POST_EVENTNAME] != nil) {
    self.eventName = [jsonObject objectForKey:BGO_POST_EVENTNAME];
  }
  if ([jsonObject objectForKey:BGO_POST_EVENTDETAIL] != nil) {
    self.eventDetail = [jsonObject objectForKey:BGO_POST_EVENTDETAIL];
  }
  if ([jsonObject objectForKey:BGO_POST_EVENTTIME] != nil) {
    _eventTime = [[jsonObject objectForKey:BGO_POST_EVENTTIME] retain];
  }
  if ([jsonObject objectForKey:BGO_POST_OPERATORCONNECTIONTYPE] != nil) {
    self.connectionType = [jsonObject objectForKey:BGO_POST_OPERATORCONNECTIONTYPE];
  }
  if ([jsonObject objectForKey:BGO_POST_OPERATORCONNECTIONTYPE] != nil) {
    self.operatorConnectionType = [jsonObject objectForKey:BGO_POST_OPERATORCONNECTIONTYPE];
  }
  if ([jsonObject objectForKey:BGO_POST_ERRORID] != nil) {
    self.errorId = [jsonObject objectForKey:BGO_POST_ERRORID];
  }
  if ([jsonObject objectForKey:BGO_POST_ERRORLEVEL] != nil) {
    _errorLevel = [[jsonObject objectForKey:BGO_POST_ERRORLEVEL] retain];
  }
  if ([jsonObject objectForKey:BGO_POST_EVENTVALUE] != nil) {
    self.eventValue = [jsonObject objectForKey:BGO_POST_EVENTVALUE];
  }
  if ([jsonObject objectForKey:@"eventPosted"] != nil) {
    _eventPosted = [[jsonObject objectForKey:@"eventPosted"] retain];
  }
}

- (void) dealloc {
  [_eventParameters release];
  [_profile release];
  [_eventName release];
  [_eventDetail release];
  [_eventTime release];
  [_connectionType release];
  [_operatorConnectionType release];
  [_errorId release];
  [_errorLevel release];
  [_eventValue release];
  [_eventPosted release];
  [super dealloc];
}
@end